package com.sctvcloud.beichuan.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.OnItemInternalCheckChangedListener;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.sctvcloud.beichuan.R;
import com.sctvcloud.beichuan.base.BaseActivity;
import com.sctvcloud.beichuan.beans.FocusColumn;
import com.sctvcloud.beichuan.ui.adapter.AddCollumnAdapter;
import com.sctvcloud.beichuan.ui.fragment.HomeFragment;
import com.sctvcloud.beichuan.ui.utils.TitleUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 栏目编辑页面
 */
public class AddColumnActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.title_top_edit)
    protected TextView top_save;
    @BindView(R.id.add_column_choice_list)
    protected RecyclerView add_column_choice;
    private GridLayoutManager manager;
    private AddCollumnAdapter addCollumnAdapter;
    private Set<FocusColumn> set;
    private ArrayList<FocusColumn> al_column;
    public static final String COLUMN = "column";
    public static final int RESULT_CODE = 2;
    public static final String SELECT_POS = "select_pos";
    private ArrayList<FocusColumn> column_data;
    private Gson gson;
    public static final String COLUMN_LIST = "column_list_id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        column_data = (ArrayList<FocusColumn>) getIntent().getSerializableExtra(HomeFragment.COLUMN_DATA);
        new TitleUtils(this).setBackClickListener(this);
        top_save.setVisibility(View.VISIBLE);
        top_save.setText(getString(R.string.save));
        top_save.setOnClickListener(this);
        manager = new GridLayoutManager(this, 3);
        add_column_choice.setLayoutManager(manager);
        add_column_choice.setItemAnimator(new DefaultItemAnimator());
        initData();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_add_column);
        ButterKnife.bind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_top_edit:
                onBackPressed();
                break;
            case R.id.title_top_back:
                onBackPressed();
                break;
        }
    }

    private OnItemInternalClick onColumnItemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            FocusColumn focus = (FocusColumn) addCollumnAdapter.getItem(position);
            for (int i = 0; i < column_data.size(); i++) {
                if (focus.getDataId().equals(column_data.get(i).getDataId())) {
                    column_data.get(i).setChoice(true);
//                    setResultes(i);
                }
            }

        }
    };
    private OnItemInternalCheckChangedListener onItemInternalCheckChangedListener = new OnItemInternalCheckChangedListener() {
        @Override
        public void onItemInternalCheckedChanged(View itemView, View checkable, int pos, boolean checked) {
            FocusColumn focus = (FocusColumn) addCollumnAdapter.getItem(pos);
            if (focus == null) {
                return;
            }
            focus.setChoice(checked);
            JLog.e("====check==" + checked);
//            setResultes(focus.getTempPos());
//            addCollumnAdapter.notifyDataSetChanged();
//            for (int i = 0; i < column_data.size(); i++) {
//                if (focus.getDataId().equals(column_data.get(i).getDataId())) {
//                    column_data.get(i).setChoice(checked);
//                    setResultes(i);
//                }
//            }
        }
    };

    @Override
    public void onBackPressed() {
        setResultes(-1);
        super.onBackPressed();
    }

    private void setResultes(int selectPos) {
        String s = initGson().toJson(column_data);
        SharedPreferencesUtil.setParam(this, COLUMN_LIST, s);
        Intent intent = getIntent();
        intent.putExtra(SELECT_POS, selectPos);
        intent.putExtra(COLUMN, (Serializable) column_data);
        setResult(RESULT_CODE, intent);
        finish();
    }

    private Gson initGson() {
        if (gson == null) {
            gson = new Gson();
        }
        return gson;
    }

    private void initData() {
        al_column = new ArrayList<>();
        if(ListUtils.isListValued(column_data)) {
            if (column_data.size() > 7) {
                for (int i = 7; i < column_data.size(); i++) {
                    FocusColumn focusColumn = column_data.get(i);
                    al_column.add(focusColumn);
                }

            } else {
                al_column = null;
            }
            if (al_column != null) {
                if (addCollumnAdapter == null) {
                    addCollumnAdapter = new AddCollumnAdapter(this, al_column)
                            .setItemInternalCheckChangedListener(onItemInternalCheckChangedListener)
                            .setItemInternalClick(onColumnItemClick);
                    add_column_choice.setAdapter(addCollumnAdapter);
                } else {
                    addCollumnAdapter.setData(al_column);
                }
            }
        }
    }
}

